package com.handinglian.ai.template.service.impl;

import com.github.pagehelper.PageHelper;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.exception.BizException;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.common.util.GeneralUtil;
import com.handinglian.ai.notice.service.NoticeService;
import com.handinglian.ai.template.dto.MessageTemplateDto;
import com.handinglian.ai.template.entity.MessageTemplate;
import com.handinglian.ai.template.entity.MessageTemplate;
import com.handinglian.ai.template.mapper.MessageTemplateMapper;
import com.handinglian.ai.template.mapper.MessageTemplateMapper;
import com.handinglian.ai.template.param.MessageTemplateCreateParam;
import com.handinglian.ai.template.param.MessageTemplateUpdateParam;
import com.handinglian.ai.template.service.MessageTemplateService;
import com.handinglian.ai.template.service.MessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class MessageTemplateServiceImpl implements MessageTemplateService {
    @Autowired
    private MessageTemplateMapper messageTemplateMapper;
    @Autowired
    private NoticeService noticeService;

    @Override
    @Transactional
    public int create(MessageTemplateCreateParam createParam) {
        MessageTemplate callTemplate = FastJsonUtil.ObjectToObject(createParam, MessageTemplate.class);
        callTemplate.setCreateTime(new Date());
        return messageTemplateMapper.insertSelective(callTemplate);
    }

    @Override
    public CommonPage<MessageTemplateDto> listPage(String nameOrContent, String type, String sortField, String sortOrder, Integer pageNum, Integer pageSize) {
        List<Integer> types = GeneralUtil.stringToIntegerList(type);
        PageHelper.startPage(pageNum, pageSize);
        List<MessageTemplateDto> messageTemplateDtos = messageTemplateMapper.list(nameOrContent, types, sortField, sortOrder);
        return CommonPage.restPage(messageTemplateDtos);
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        Integer count = noticeService.countMessageTemplate(id);
        if (count > 0){
            throw BizException.TEMPLATE_USED_ERROR;
        }
        return messageTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int update(MessageTemplateUpdateParam updateParam) {
        MessageTemplate callTemplate = FastJsonUtil.ObjectToObject(updateParam, MessageTemplate.class);
        return messageTemplateMapper.updateByPrimaryKeySelective(callTemplate);
    }

    @Override
    public MessageTemplateDto getDetail(Integer id) {
        MessageTemplate callTemplate = messageTemplateMapper.selectByPrimaryKey(id);
        return FastJsonUtil.ObjToObjWithDateFormat(callTemplate, MessageTemplateDto.class);
    }

    @Override
    public int setDefault(Integer id) {
        messageTemplateMapper.updateIsDefault();
        return messageTemplateMapper.updateIsDefaultById(id);
    }

    @Override
    @Transactional
    public int deleteBatch(String id) {
        List<Integer> ids = GeneralUtil.stringToIntegerList(id);
        return messageTemplateMapper.deleteByIdIn(ids);
    }

    @Override
    public List<MessageTemplateDto> list(Integer type) {
        return messageTemplateMapper.findByTypeOrderByIsDefaultDesc(type);
    }
}
